-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(libwaku): async #3180
base: master
Are you sure you want to change the base?
refactor(libwaku): async #3180
Conversation
You can find the image built from this PR at
Built from 9a0b46b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for the magic! 🥳
I just added a few comments that I hope you find useful :)
setupForeignThreadGc() | ||
|
||
body | ||
template checkLibwakuParams*( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should also move this template to the ffi_types
module
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this template depends on WakuContext
we would end up with a import cycle.
library/libwaku.nim
Outdated
let msg = $error | ||
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData) | ||
return nil | ||
discard waku_thread |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think is interesting to handle any possible error, and in case of error, return nil
:)
library/libwaku.nim
Outdated
let res = waku_thread.destroyWakuThread(ctx) | ||
if res.isErr(): | ||
foreignThreadGc: | ||
let msg = "libwaku error: " & $res.error | ||
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData) | ||
return RET_ERR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could use the isOkOr
and also return RET_OK
if all goes well ;P
let res = waku_thread.destroyWakuThread(ctx) | |
if res.isErr(): | |
foreignThreadGc: | |
let msg = "libwaku error: " & $res.error | |
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData) | |
return RET_ERR | |
waku_thread.destroyWakuThread(ctx).isOkOr: | |
foreignThreadGc: | |
let msg = "libwaku error: " & $error | |
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData) | |
return RET_ERR | |
return RET_OK |
@@ -28,22 +29,79 @@ type RequestType* {.pure.} = enum | |||
type InterThreadRequest* = object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside from this PR but I think it would make more sense to rename this type to WakuThreadRequest
, if you agree :)
deallocShared(request) | ||
let fireSyncRes = request[].doneSignal.fireSync() | ||
if fireSyncRes.isErr(): | ||
let msg = "libwaku error: " & $fireSyncRes.error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Give a little bit more context
let msg = "libwaku error: " & $fireSyncRes.error | |
let msg = "libwaku error: handleRes fireSyncRes error: " & $fireSyncRes.error |
|
||
proc deallocOnDone*(T: type InterThreadRequest, request: ptr InterThreadRequest) = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we could make this private and invoke it within a defer
block inside the handleRes
proc.
proc deallocOnDone*(T: type InterThreadRequest, request: ptr InterThreadRequest) = | |
proc deallocOnDone(T: type InterThreadRequest, request: ptr InterThreadRequest) = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm not sure how at the moment. I'm using this deallocOnDone() function as a way to get the functions in libwaku.nim to 'wait' until there's a result and dispose of the waku request afterwards 🤔
) | ||
return | ||
|
||
discard request[].doneSignal.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could handle any possible error
discard request[].doneSignal.close() | |
request[].doneSignal.close().isOkOr: | |
foreignThreadGc: | |
let msg = "libwaku error: " & $res.error | |
request[].callback( | |
RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), request[].userData | |
) |
return | ||
|
||
discard request[].doneSignal.close() | ||
deallocShared(request) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd move that line to the top of the proc within a defer
block
|
||
proc process*( | ||
T: type InterThreadRequest, request: ptr InterThreadRequest, waku: ptr Waku | ||
) {.async.} = | ||
echo "Request received: " & $request[].reqType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR but I think we can remove this line:)
library/libwaku.nim
Outdated
|
||
proc handleRes[T: string | void]( | ||
res: Result[T, string], callback: WakuCallBack, userData: pointer | ||
proc handleSentToChannelRes( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems as if this proc is no longer needed because we are handling the responses within the waku_thread_request
module. I see some room for simplification by completely removing that one :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a small refactor. Should simplify a bit the code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazingg, thanks so much! 🔥 🔥
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks for it! 🙌
Description
Allows the execution of libwaku functions asynchronisly instead of executing each request linearly in the event loop of
runWaku
.